package com.qt.myio;

import java.io.*;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Author zsf
 * @Date 2023/10/2 0:47
 * @Version 1.0
 */
public class PropertiesDemo01 {

    public static void main(String[] args) throws IOException {

        Properties prop = new Properties();
        prop.put("zhangsan","aaa");
        prop.put("zhangsan","qqq");
        prop.put("name","zsf");

        prop.setProperty("aaa","123");

        //1.写入 Properties集合到文件中------
        //第二个参数表示文件注释
        FileOutputStream fos = new FileOutputStream("aaa\\a.properties");
        FileWriter fw = new FileWriter("aaa\\b.properties");
        prop.store(fos,"aaa");
        prop.store(fw,"bbb");
        fos.close();
        fw.close();

        //2.从文件中读取Properties集合
        FileInputStream fis = new FileInputStream("aaa\\a.properties");
        FileReader fr = new FileReader("aaa\\b.properties");
        prop.load(fis);
        prop.load(fr);
        fis.close();
        fr.close();

        //3.遍历
        //3.1 keySet
        Set<Object> keysets = prop.keySet();
        for (Object key : keysets) {
            System.out.println(key+"="+prop.get(key));
        }
        //3.2 entrySet
        Set<Map.Entry<Object, Object>> entries = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key+"="+value);
        }


    }

}
